package net.thxjj.adminServer.mongodao;

import net.thxjj.adminServer.entity.LoginHadleEvent;
import net.thxjj.adminServer.entity.aep.profilegc.ProfileGCLoginBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class MongoProfileGCLoginDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveProfileGCLoginBean(ProfileGCLoginBean profileGCLoginBean){
        ProfileGCLoginBean po = findGCLoginBeanByDeviceIdAndTime(profileGCLoginBean.getDeviceId(),profileGCLoginBean.getTimestamp());
        if(po==null){
            mongoTemplate.save(profileGCLoginBean);
        }else{
            updateGCLoginBean(profileGCLoginBean);
        }
    }

    //导入校验（如果有timestamp和deviceId重合则更新）
    public void saveProfileGCLoginBeans(List<ProfileGCLoginBean> list){
        for (ProfileGCLoginBean profile:
                list) {
            ProfileGCLoginBean po = findGCLoginBeanByDeviceIdAndTime(profile.getDeviceId(),profile.getTimestamp());
            if(po==null){
                mongoTemplate.save(profile);
            }else{
                updateGCLoginBean(profile);
            }
        }
    }


    private ProfileGCLoginBean findGCLoginBeanByDeviceIdAndTime(String deviceId, long time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).and("timestamp").is(time));
        return mongoTemplate.findOne(query, ProfileGCLoginBean.class);
    }

    private void updateGCLoginBean(ProfileGCLoginBean profileGCLoginBean) {


        Query query = new Query(Criteria.where("deviceId").is(profileGCLoginBean.getDeviceId()).and("timestamp").is(profileGCLoginBean.getTimestamp()));
        Update update = new Update()
                .set("Device_Type",profileGCLoginBean.getDevice_Type())
                .set("TAU_Flag",profileGCLoginBean.getTAU_Flag())
                .set("TAU",profileGCLoginBean.getTAU())
                .set("MD_HwVersion",profileGCLoginBean.getMD_HwVersion())
                .set("MD_FwVersion",profileGCLoginBean.getMD_FwVersion())
                .set("SD_HwVersion",profileGCLoginBean.getSD_HwVersion())
                .set("SD_SwVersion",profileGCLoginBean.getSD_SwVersion())
                .set("SD_PtVersion",profileGCLoginBean.getSD_PtVersion())
                .set("APP_SwVersion",profileGCLoginBean.getAPP_SwVersion())
                .set("IMSI",profileGCLoginBean.getIMSI())
                .set("IMEI",profileGCLoginBean.getIMEI())
                .set("Custom_Msg",profileGCLoginBean.getCustom_Msg())
                .set("Custom_Msg_Length",profileGCLoginBean.getCustom_Msg_Length());
        mongoTemplate.upsert(query,update, ProfileGCLoginBean.class);

    }

    public ProfileGCLoginBean getNewestLoginInfo(String deviceId){
        Query query = new Query(Criteria.where("deviceId").is(deviceId));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query,ProfileGCLoginBean.class);
    }

    public List<ProfileGCLoginBean> getLoginInfoByTime(String device_id,long begin_time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("timestamp").gte(begin_time));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query,ProfileGCLoginBean.class);
    }

    public List<ProfileGCLoginBean> getLoginDataAtTime(String device_id, long start_time, long end_time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time)));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query,ProfileGCLoginBean.class);
    }


    private List<ProfileGCLoginBean> getAllLoginInfo(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        return mongoTemplate.find(query,ProfileGCLoginBean.class);
    }

    public List<LoginHadleEvent> getLoginHandleWithLog(String device_id){
        List<ProfileGCLoginBean> list=  getAllLoginInfo(device_id);
        ArrayList<LoginHadleEvent> list1 = new ArrayList<>();
        for (ProfileGCLoginBean logindata:
                list) {
            LoginHadleEvent loginHadleEvent = new LoginHadleEvent().setDeviceId(logindata.getDeviceId()).setTimestamp(logindata.getTimestamp()).setUserId(0);
            list1.add(loginHadleEvent);
        }
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        List<LoginHadleEvent> list2 = mongoTemplate.find(query,LoginHadleEvent.class);
        list1.addAll(list2);
        Collections.sort(list1);
        return list1;
    }

}
